package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * sky-take-out
 * com.sky.controller.user <br>
 * C端购物车控制器
 * @author Ablecisi
 * @version 1.0
 * 2025/4/10
 * 15:10
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车相关接口")
public class ShoppingCartController {
    private final ShoppingCartService shoppingCartService;

    @Autowired
    public ShoppingCartController(ShoppingCartService shoppingCartService) {
        this.shoppingCartService = shoppingCartService;
    }

    /**
     * 查询购物车
     * @return 购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public Result<List<ShoppingCart>> list() {
        log.info("查询购物车");
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        return Result.success(shoppingCartList);
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车数据传输对象
     * @return 添加结果
     */
    @PostMapping("add")
    @ApiOperation("添加购物车")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车，参数：{}", shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success("添加购物车成功");
    }

    /**
     * 清空购物车
     * @return 清空结果
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean() {
        log.info("清空购物车");
        shoppingCartService.clean();
        return Result.success("清空购物车成功");
    }

    /**
     * 删除购物车
     * @param shoppingCartDTO 购物车数据传输对象
     * @return 删除结果
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车")
    public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车，参数：{}", shoppingCartDTO);
        shoppingCartService.sub(shoppingCartDTO);
        return Result.success("删除购物车成功");
    }
}
